package com.qianbao.qianbaobusiness.ui.support.wechatprogram;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidbase.widget.recycleadpter.BaseRecycleViewAdapter;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.MerchantInfo;

/**
 * @author: Routee
 * @date 2018/6/21
 * @mail wangc4@qianbaocard.com
 * ------------1.本类由Routee开发,阅读、修改时请勿随意修改代码排版格式后提交到git。
 * ------------2.阅读本类时，发现不合理请及时指正.
 * ------------3.如需在本类内部进行修改,请先联系Routee,若未经同意修改此类后造成损失本人概不负责。
 */
public class SingleSwitchShopRvAdapter extends BaseRecycleViewAdapter<MerchantInfo> {
    private String      mMerchantNo;
    private ClickListen mListener;

    public SingleSwitchShopRvAdapter(Context context, String merchantNo) {
        super(context);
        mMerchantNo = merchantNo;
    }

    @Override
    protected void onBindBaseViewHolder(RecyclerView.ViewHolder holder, final MerchantInfo item) {
        final VH vh = (VH) holder;
        vh.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null) {
                    mListener.click(item.merchantNo);
                }
                mMerchantNo = item.merchantNo;
                notifyDataSetChanged();
            }
        });
        vh.mTv.setText(item.merchantName);
        if (mMerchantNo.equals(item.merchantNo)) {
            vh.mTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_red));
            vh.mIv.setVisibility(View.VISIBLE);
        } else {
            vh.mTv.setTextColor(ContextCompat.getColor(mContext, R.color.text_important));
            vh.mIv.setVisibility(View.GONE);
        }
        switch (vh.getItemViewType()) {
            case 0:
                vh.mSeparator.setVisibility(View.VISIBLE);
                break;
            case 1:
                vh.mSeparator.setVisibility(View.VISIBLE);
                break;
            case 2:
                vh.mSeparator.setVisibility(View.GONE);
                break;
            default:
                break;
        }
        vh.mTvAddress.setText(item.shopProviceName + "-" + item.shopCityName + "-" + item.shopDistrictName);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateBaseViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_single_switch_shop, null);
        return new VH(view);
    }

    @Override
    protected int getBaseItemViewType(int position) {
        int lastIndex = mData.size() - 1;
        if (position == 0) {
            return 0;
        } else if (position == lastIndex) {
            return 2;
        } else {
            return 1;
        }
    }

    class VH extends RecyclerView.ViewHolder {

        ImageView mIv;
        TextView  mTv;
        View      mSeparator;
        TextView  mTvAddress;

        public VH(View itemView) {
            super(itemView);
            mIv = itemView.findViewById(R.id.iv);
            mTv = itemView.findViewById(R.id.tv_name);
            mTvAddress = itemView.findViewById(R.id.tv_address);
            mSeparator = itemView.findViewById(R.id.separator);
        }
    }

    public interface ClickListen {
        void click(String o);
    }

    public void setOnClickListen(ClickListen listen) {
        mListener = listen;
    }
}

